package com.silence.gankio.fragment

import android.arch.lifecycle.Observer
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import com.silence.gankio.R
import com.silence.gankio.adapter.GankWealAdapter
import com.silence.gankio.bean.GankIoData
import com.silence.gankio.mvvm.viewModel.GankWealViewModel
import kotlinx.android.synthetic.main.gank_android_fragment.*

class GankWealFragment : GankIoFragment<GankWealViewModel>(), SwipeRefreshLayout.OnRefreshListener {

    override fun getFragmentContentView() = R.layout.gank_weal_fragment

    private val mAdapter by lazy { GankWealAdapter(null) }
    private var page = 1
    private var first = true

    companion object {
        fun newInstance() = GankWealFragment()
    }

    override fun getModelClass() = GankWealViewModel::class.java


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initListener()

        refresh_layout.post {
            refresh_layout.isRefreshing = true
            loadData(true)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        staggeredGridLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
        recycler.layoutManager = staggeredGridLayoutManager
        recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                staggeredGridLayoutManager.invalidateSpanAssignments()
            }
        })
        mAdapter.openLoadAnimation()
        // 当列表滑动到倒数第N个Item的时候(默认是1)回调onLoadMoreRequested方法
        mAdapter.setPreLoadNumber(5)
        mAdapter.setHasStableIds(true)

        recycler.adapter = mAdapter

        refresh_layout.setOnRefreshListener(this)
    }

    private fun loadData(showLoadingUI: Boolean) {
        mBaseModel?.loadData(showLoadingUI, page, 10)
    }

    override fun onRefresh() {
        if (mAdapter.isLoading) {
            return
        }
        page = 1
        first = true
        loadData(false)
    }

    private fun initListener() {
        mBaseModel?.getWealData()?.observe(this,
            Observer<List<GankIoData>> { t ->
                mAdapter.loadMoreComplete()
                if (t.isNullOrEmpty()) {
                    mAdapter.loadMoreFail()
                } else {
                    page++
                    if (first) {
                        first = false
                        mAdapter.setNewData(t)
                        mAdapter.disableLoadMoreIfNotFullPage(recycler)
                    } else {
                        mAdapter.addData(t)
                    }
                }
                refresh_layout.isRefreshing = false
                mAdapter.setEnableLoadMore(true)
            })

        mAdapter.setOnLoadMoreListener({
            loadData(false)
        }, recycler)

        mAdapter.setOnItemClickListener { adapter, view, position ->

        }
    }

}
